package com.example.apisimpledy.controller;

import com.example.apisimpledy.entity.RestBean;
import com.example.apisimpledy.entity.dto.Comment;
import com.example.apisimpledy.entity.dto.Video;
import com.example.apisimpledy.entity.vo.VideoVO;
import com.example.apisimpledy.entity.vo.CommentVO;
import com.example.apisimpledy.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/videos")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @PreAuthorize("hasRole('user')")
    @PostMapping("/upload")
    public String uploadVideo(@RequestParam("videoFile") MultipartFile videoFile) {
        try {
            Video video = videoService.uploadVideo(videoFile);
            VideoVO videoVO = new VideoVO().setId(video.getId())
                    .setTitle(video.getTitle())
                    .setDescription(video.getDescription())
                    .setUploadTime(video.getUploadTime())
                    .setVideoPath(video.getVideoPath())
                    .setThumbnailPath(video.getThumbnailPath())
                    .setDuration(video.getDuration())
                    .setLikes(video.getLikes())
                    .setViews(video.getViews());
            return RestBean.success(videoVO).asJsonString();
        } catch (RuntimeException e) {
            return RestBean.failure(400, e.getMessage()).asJsonString();
        }
    }

    @PreAuthorize("hasRole('uploader')")
    @DeleteMapping("/delete/{videoId}")
    public String deleteVideo(@PathVariable Long videoId) {
        try {
            videoService.deleteVideo(videoId);
            return RestBean.success().asJsonString();
        } catch (RuntimeException e) {
            return RestBean.failure(400, e.getMessage()).asJsonString();
        }
    }

    @PreAuthorize("hasRole('uploader')")
    @GetMapping("/my")
    public String getMyVideos(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {
        try {
            List<Video> videos = videoService.getMyVideos(page, size);
            List<VideoVO> videoVOS = videos.stream().map(video -> new VideoVO()
                            .setId(video.getId())
                            .setTitle(video.getTitle())
                            .setDescription(video.getDescription())
                            .setUploadTime(video.getUploadTime())
                            .setVideoPath(video.getVideoPath())
                            .setThumbnailPath(video.getThumbnailPath())
                            .setDuration(video.getDuration())
                            .setLikes(video.getLikes())
                            .setViews(video.getViews()))
                    .collect(Collectors.toList());
            System.out.println(videoVOS);
            return RestBean.success(videoVOS).asJsonString();
        } catch (RuntimeException e) {
            return RestBean.failure(400, e.getMessage()).asJsonString();
        }
    }

    @PreAuthorize("hasRole('user')")
    @PostMapping("/{videoId}/like")
    public String likeVideo(@PathVariable Long videoId) {
        try {
            String result = videoService.likeVideo(videoId);
            return result.equals("视频成功点赞") ? RestBean.success(result).asJsonString() : RestBean.failure(400, result).asJsonString();
        } catch (RuntimeException e) {
            return RestBean.failure(404, e.getMessage()).asJsonString();
        }
    }

    @PreAuthorize("hasRole('user')")
    @GetMapping("/recommend")
    public String getRecommendedVideos() {
        try {
            List<Video> videos = videoService.getRecommendedVideos();
            List<VideoVO > videoVOS = videos.stream().map(video -> new VideoVO()
                            .setId(video.getId())
                            .setTitle(video.getTitle())
                            .setDescription(video.getDescription())
                            .setUploadTime(video.getUploadTime())
                            .setVideoPath(video.getVideoPath())
                            .setThumbnailPath(video.getThumbnailPath())
                            .setDuration(video.getDuration())
                            .setLikes(video.getLikes())
                            .setViews(video.getViews()))
                    .toList();
            return RestBean.success(videoVOS).asJsonString();
        } catch (RuntimeException e) {
            return RestBean.failure(400, e.getMessage()).asJsonString();
        }
    }

    @PreAuthorize("hasRole('user')")
    @PostMapping("/comments/addOne")
    public String addComment(@RequestParam Long videoId, @RequestParam String comment) {
        try {
            videoService.addComment(videoId, comment);
            return RestBean.success().asJsonString();
        } catch (RuntimeException e) {
            return RestBean.failure(400, e.getMessage()).asJsonString();
        }
    }

    @PreAuthorize("hasRole('userr')")
    @GetMapping("/{videoId}/comments/getAll")
    public String getComments(@PathVariable Long videoId) {
        try {
            List<Comment> comments = videoService.getComments(videoId);
            List<CommentVO > commentVOS = comments.stream()
                    .map(CommentVO::fromEntity)
                    .toList();
            return RestBean.success(commentVOS).asJsonString();
        } catch (RuntimeException e) {
            return RestBean.failure(400, e.getMessage()).asJsonString();
        }
    }

    @PreAuthorize("hasRole('user')")
    @PostMapping("/{videoId}/view")
    public String recordView(@PathVariable Long videoId) {
        try {
            videoService.recordView(videoId);
            return RestBean.success().asJsonString();
        } catch (RuntimeException e) {
            return RestBean.failure(400, e.getMessage()).asJsonString();
        }
    }
}
